package com.yu.xcx.wxweb;

import com.alibaba.fastjson.JSONObject;
import com.yu.xcx.controller.UserController;
import com.yu.xcx.dto.Result;
import com.yu.xcx.entity.SendParam;
import com.yu.xcx.entity.SendReturn;
import com.yu.xcx.entity.User;
import com.yu.xcx.service.impl.UserServiceImpl;
import com.yu.xcx.util.SendUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx")
public class WXWeb {

    @Resource
    private UserServiceImpl userService;

    @Resource
    private UserController userController;

//    @GetMapping("/getAll")
//    public Result getToken(String openId, String access_token){
//        SendParam sendParam = new SendParam();
//        sendParam.setUrl("https://api.weixin.qq.com/sns/userinfo");
//        Map<String,String> param = new HashMap();
//        param.put("access_token",access_token);
//        param.put("openid",openId);
//        param.put("lang","zh_CN");
//        sendParam.setGetParam(param);
//        SendReturn sendReturn =  SendUtils.sendGet(sendParam);
//        String result = sendReturn.getReturnString();
//        JSONObject jsonObject = JSONObject.parseObject(result);
//        return Result.ok(jsonObject);
//    }


    @GetMapping ("/getOpenId")
    public Result getOpenId(String code){
        SendParam sendParam = new SendParam();
        sendParam.setUrl("https://api.weixin.qq.com/sns/jscode2session");
        Map<String,String> param = new HashMap();
        param.put("appid","wx746c775244f127e3");
        param.put("secret","dfc5d685d7bf2c91a2349c9fd88ec927");
        param.put("js_code",code);
        param.put("grant_type","authorization_code");
        sendParam.setGetParam(param);
        SendReturn sendReturn =  SendUtils.sendGet(sendParam);
        String result = sendReturn.getReturnString();
        String[] strs = result.split(",");

        String[] strs2 = strs[0].split("\"");
        String[] strs3 = strs[1].split("\"");


        List<String> list = new ArrayList<>();
        list.add(strs2[3]);
        list.add(strs3[3]);
        return Result.ok(list);
    }

    @GetMapping("/getACToken")
    public String getACToken(){
        SendParam sendParam = new SendParam();
        sendParam.setUrl("https://api.weixin.qq.com/cgi-bin/token");
        Map<String,String> param = new HashMap();
        param.put("appid","wx746c775244f127e3");
        param.put("secret","dfc5d685d7bf2c91a2349c9fd88ec927");
        param.put("grant_type","client_credential");
        sendParam.setGetParam(param);
        SendReturn sendReturn =  SendUtils.sendGet(sendParam);
        String result = sendReturn.getReturnString();
        String[] strs = result.split(",");
        String[] strs2 = strs[0].split("\"");
        System.out.println(strs[0]);
        return strs2[3];
    }
}
